<template>
  <div class="main_spac1">
    <div class="main_white padding10 paddingTop0">
      <div class="main_ti box">
        <el-tabs v-model="activeName" @tab-click="handleClick" class="box">
          <el-tab-pane label="全部" name="-1"></el-tab-pane>
          <el-tab-pane name="0">
            <div slot="label">
              <span>待审核</span>
            </div>
          </el-tab-pane>
          <el-tab-pane label="已通过" name="1"></el-tab-pane>
          <el-tab-pane label="已驳回" name="2"></el-tab-pane>
        </el-tabs>
      </div>
      <div class="mainor_form">
        <div class="mainor_te">
          <!-- <router-link to="/order/list/add/add" class="mainor_tea">添加学校</router-link> -->
          <el-button type="primary" @click="openDialog">添加学校</el-button>
        </div>
        <el-form
          :inline="true"
          :model="form"
          class="demo-form-inline"
          ref="userListFrom"
          label-width="70px"
        >
          <el-form-item label="学校名称">
            <el-input
              v-model="form.name"
              clearable
              placeholder="请输入学校名称"
            ></el-input>
          </el-form-item>
          <el-form-item label="学校域名">
            <el-input
              v-model="form.domain_name"
              clearable
              placeholder="请输入学校域名"
            ></el-input>
          </el-form-item>
          <el-form-item label="区域搜索">
            <el-input
              v-model="form.region"
              clearable
              placeholder="请输入区域"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button class="reset marginLeft10" type="primary" @click="reset"
              >重置</el-button
            >
            <el-button
              type="primary"
              @click="getArticleList"
              :loading="listLoading"
              >搜索</el-button
            >
          </el-form-item>
        </el-form>
      </div>
      <div class="main_num boxdq"></div>
      <div class="main_table" style="min-height: 530px">
        <el-table
          ref="multipleTable"
          v-loading="listLoading"
          :data="schoolOrder"
          element-loading-text="加载中"
          fit
          border
          highlight-current-row
          :header-cell-class-name="this.$verticaline"
        >
          <el-table-column type="expand">
            <template slot-scope="props">
              <el-row>
                <el-col :span="11" class="mini_form">
                  <el-card class="box-card">
                    <el-form ref="form" inline :model="form" label-width="80px">
                      <el-col :span="12">
                        <el-form-item label="学校logo">
                          <img src="" alt="" />
                        </el-form-item>
                      </el-col>
                      <el-col :span="12">
                        <el-form-item label="学校资质">
                          <div
                            class="imgCard"
                            v-for="(item, index) in props.row.qualifications"
                            :key="index"
                          >
                            <img :src="item.url" />
                          </div>
                        </el-form-item>
                      </el-col>
                      <el-col :span="24">
                        <el-form-item label="学校简介">
                          <span>{{ props.row.desc }}</span>
                        </el-form-item>
                      </el-col>
                    </el-form>
                  </el-card>
                </el-col>
                <el-col :span="12">
                  <el-table
                    :data="props.row.tableData"
                    border
                    style="width: 100%"
                  >
                    <el-table-column prop="date" label="竞价排名">
                    </el-table-column>
                    <el-table-column prop="name" label="供应商公司">
                    </el-table-column>
                    <el-table-column prop="address" label="剩余时间">
                    </el-table-column>
                  </el-table>
                </el-col>
              </el-row>
            </template>
          </el-table-column>
          <el-table-column type="index" />
          <el-table-column
            label="学校名称"
            prop="name"
            :show-overflow-tooltip="true"
          ></el-table-column>
          <el-table-column
            label="学校负责人"
            prop="manager_name"
            :show-overflow-tooltip="true"
          ></el-table-column>
          <el-table-column
            label="学校负责人电话"
            prop="mobile"
            :show-overflow-tooltip="true"
          ></el-table-column>
          <el-table-column
            label="学校地址"
            prop="address"
            :show-overflow-tooltip="true"
            width="250"
          ></el-table-column>
          <el-table-column label="审核结果" prop="nickname">
            <template slot-scope="scope">
              <el-tag
                :type="
                  scope.row.audit_status == 0
                    ? 'warning'
                    : scope.row.audit_status == 1
                    ? 'success'
                    : 'danger'
                "
                >{{
                  scope.row.audit_status == 0
                    ? "待审核"
                    : scope.row.audit_status == 1
                    ? "审核成功"
                    : "审核失败"
                }}</el-tag
              >
            </template>
          </el-table-column>
          <el-table-column
            label="创建时间"
            prop="created_at"
            :show-overflow-tooltip="true"
            width="200"
          ></el-table-column>
          <el-table-column label="学校状态" prop="status">
            <template slot-scope="scope">
              <el-switch
                v-model="scope.row.status"
                :disabled="disabled"
                active-color="#13ce66"
                inactive-color="#ff4949"
                :active-value="1"
                :inactive-value="0"
                @change="changeSwitch($event, scope.row)"
              >
              </el-switch>
            </template>
          </el-table-column>
          <el-table-column
            label="管理操作"
            align="left"
            fixed="right"
            width="200"
            class-name="main_hei"
          >
            <template slot-scope="scope">
              <el-button size="mini" type="text" @click="navTo(scope.row)"
                >学校详情</el-button
              >
              <el-button size="mini" type="text" @click="account(scope.row.id)"
                >账号</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="main_page boxdq">
        <!-- 分页 -->
        <div class="main_page_r">
          <el-pagination
            :current-page="current.page"
            :page-sizes="[20, 30, 50, 100]"
            :page-size="current.limit"
            layout="total, sizes, prev, pager, next, jumper"
            :total="totalCount"
            background
            @size-change="changeLimit"
            @current-change="changePage"
          />
        </div>
      </div>
    </div>

    <!-- 学校添加模态框 -->
    <el-dialog
      :title="schoolTitle"
      :visible.sync="schoolDialog"
      width="70%"
      :close-on-click-modal="false"
      @close="closeCard"
    >
      <el-row>
        <el-form
          :model="ruleForm"
          :rules="rules"
          ref="ruleForm"
          label-width="150px"
          class="demo-ruleForm"
        >
          <el-col :span="24"
            ><el-divider content-position="left">基础信息</el-divider></el-col
          >
          <el-col :span="12">
            <el-form-item label="学校名称" prop="name">
              <el-input
                class="input_width"
                v-model="ruleForm.name"
                placeholder="请输入学校名称"
                clearable
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="学校地址" prop="code">
              <el-cascader
                class="input_width"
                v-model="ruleForm.code"
                placeholder="请选择学校省市区"
                :options="options"
                :props="props"
                @change="handleChange"
                clearable
              ></el-cascader>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="详细地址" prop="address">
              <el-input
                type="textarea"
                placeholder="请输入详细地址"
                :autosize="{ minRows: 2, maxRows: 4 }"
                v-model="ruleForm.address"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="学校负责人" prop="manager_name">
              <el-input
                class="input_width"
                clearable
                v-model="ruleForm.manager_name"
                placeholder="请输入学校负责人"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="负责人手机" prop="mobile">
              <el-input
                class="input_width"
                clearable
                v-model="ruleForm.mobile"
                placeholder="请输入负责人手机"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="学校状态" prop="status">
              <el-select
                class="input_width"
                v-model="ruleForm.status"
                placeholder="请选择学校状态"
              >
                <el-option label="暂停服务" :value="0"></el-option>
                <el-option label="正常" :value="1"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <!-- <el-col :span="12">
						<el-form-item label="审核结果" prop="name">
							<el-tag type="success">通过</el-tag>
						</el-form-item>
					</el-col> -->
          <el-col :span="24"
            ><el-divider content-position="left">详细信息</el-divider></el-col
          >
          <el-col :span="12">
            <el-form-item label="logo上传" prop="logo">
              <el-upload
                :action="uploadPath"
                list-type="picture-card"
                :data="{ store: 'saas/contract' }"
                style="min-height: 120px"
                :on-success="
                  (response, file, fileList) => {
                    successLogo(response, file, fileList, 'logo');
                  }
                "
                :on-preview="handlePictureCardPreview"
                :on-remove="handleRemove"
                :file-list="ruleForm.logo"
                :on-error="onError"
                :on-progress="onProgress"
              >
                <span slot="default">
                  <svg
                    aria-hidden="true"
                    class="main_page_svg"
                    width="32"
                    height="27"
                  >
                    <use xlink:href="#icon-commodit_camera" />
                  </svg>
                </span>
              </el-upload>
              <el-dialog :visible.sync="dialogVisible" append-to-body>
                <img width="100%" :src="dialogImageUrl" alt="" />
              </el-dialog>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="资质上传" prop="qualifications">
              <el-upload
                :action="uploadPath"
                list-type="picture-card"
                :data="{ store: 'saas/contract' }"
                style="min-height: 120px"
                :on-success="
                  (response, file, fileList) => {
                    successLogo(response, file, fileList, 'qualifications');
                  }
                "
                :on-preview="handlePictureCardPreview"
                :on-remove="handleRemove"
                :file-list="ruleForm.qualifications"
                :on-error="onError"
                :on-progress="onProgress"
              >
                <span slot="default">
                  <svg
                    aria-hidden="true"
                    class="main_page_svg"
                    width="32"
                    height="27"
                  >
                    <use xlink:href="#icon-commodit_camera" />
                  </svg>
                </span>
              </el-upload>
              <el-dialog :visible.sync="dialogVisible">
                <img width="100%" :src="dialogImageUrl" alt="" />
              </el-dialog>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="学校简介" prop="desc">
              <el-input
                type="textarea"
                placeholder="请输入学校简介"
                :autosize="{ minRows: 2, maxRows: 4 }"
                v-model="ruleForm.desc"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addSchool">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { Debounce } from "@/utils/index.js";
// 导入省市区json文件
import { lists } from "@/assets/json/area_2.json";
// 导入上传图片的文件
import { uploadPath } from "@/api/currency";
// 导入接口
import {
  // 新增、修改、删除学校接口
  addSchool,
  // 搜索学校列表接口
  searchSchool,
} from "@/api/order/orderList";

import dateOption from "@/options/dateOption";
import { apiUrl, objKeySort, Md5 } from "@/utils/public";
import { parseTime } from "@/utils/index";
import { mapGetters } from "vuex";
export default {
  computed: {
    ...mapGetters(["avatar"]),
  },
  data() {
    return {
      disabled: false,
      split_chec: [],
      // 时间
      parseTime,
      // 分页
      current: {
        page: 1,
        limit: 20,
      },
      totalCount: 0,

      listLoading: false,
      // 搜索
      form: {
        name: null,
        domain_name: null,
        region: null,
      },
      createTime: [],
      sendTime: [],
      pickerOptions: dateOption,
      // 选中菜单
      activeName: "-1",
      type: [],

      value: null,

      // 检索中的学校列表
      schoolList: [
        {
          value: "选项1",
          label: "黄金糕",
        },
        {
          value: "选项2",
          label: "双皮奶",
        },
        {
          value: "选项3",
          label: "蚵仔煎",
        },
        {
          value: "选项4",
          label: "龙须面",
        },
        {
          value: "选项5",
          label: "北京烤鸭",
        },
      ],
      // 列表中的学校列表
      schoolOrder: [],
      // 添加学校时的模态框状态
      schoolDialog: false,
      // 添加学校时模态框的表单信息
      ruleForm: {
        // 学校名称
        name: null,
        // 学校省市区
        code: [],
        // 省编号
        province_code: null,
        // 市编号
        city_code: null,
        // 县编号
        country_code: null,
        // 详细地址
        address: null,
        // 负责人
        manager_name: null,
        // 负责人手机
        mobile: null,
        // 学校状态(0为暂停服务，1为正常)
        status: 1,
        // logo上传
        logo: [],
        // 资质上传
        qualifications: [],
        // 简介
        desc: null,
      },
      // 添加学校时模态框的表单验证信息
      rules: {
        name: [{ required: true, message: "请输入学校名称", trigger: "blur" }],
        code: [
          { required: true, message: "请选择学校地址", trigger: "change" },
        ],
        address: [
          { required: true, message: "请输入学校名称", trigger: "blur" },
        ],
        manager_name: [
          { required: true, message: "请输入学校负责人", trigger: "blur" },
        ],
        mobile: [
          { required: true, message: "请输入负责人手机", trigger: "blur" },
        ],
        status: [
          { required: true, message: "请选择学校状态", trigger: "change" },
        ],
        logo: [
          { required: true, message: "请上传logo图片", trigger: "change" },
        ],
        qualifications: [
          { required: true, message: "请上传资质图片", trigger: "change" },
        ],
        desc: [{ required: true, message: "请输入学校简介", trigger: "blur" }],
      },
      // 学校的省市区地址
      options: lists,
      // 上传图片的接口
      uploadPath,
      // 学校的省市区地址规则
      props: {
        value: "id",
        label: "name",
      },
      filelist: [],
      // 上传图片的浏览模态框
      dialogVisible: false,
      // 上传图片的浏览链接
      dialogImageUrl: null,
      // 添加学校时模态框的标题
      schoolTitle: null,
    };
  },
  created() {
    this.getSchoolList();
  },
  methods: {
    // 获取学校接口
    getSchoolList() {
      this.listLoading = true;
      let data = {
        name: this.form.name,
        domain_name: this.form.domain_name,
        page: this.current.page,
        per_page: this.current.limit,
        audit_status: this.activeName,
        name: this.form.name,
        domain_name: this.form.domain_name,
      };
      let param = Object.assign(
        {
          sign: Md5(
            objKeySort(Object.assign(apiUrl().name, data)) + apiUrl().appsecret
          ),
        },
        apiUrl().name,
        data
      );
      searchSchool(param).then((res) => {
        if (res.errcode == 0) {
          this.schoolOrder = res.data.list;
          this.totalCount = res.data.count;
          this.listLoading = false;
        } else {
          this.$message.error(res.errmsg);
        }
      });
    },
    // 选中菜单
    handleClick(tab, event) {
      this.current.page = 1;
      this.current.limit = 20;
      this.getSchoolList();
    },
    // 搜索
    getArticleList() {
      this.current.page = 1;
      this.getSchoolList();
    },
    // 重置
    reset() {
      this.$refs["userListFrom"].resetFields();
      if (this.$refs["changeGoodsInput"]) {
        this.$refs["changeGoodsInput"].name = "";
      }
      if (this.$refs["changeUserInput_form"]) {
        this.$refs["changeUserInput_form"].name = "";
      }
      this.createTime = [];
      this.sendTime = [];
      // this.order_index();
    },
    // 添加学校
    openDialog() {
      this.schoolTitle = "新增学校";
      this.schoolDialog = true;
    },
    // 切换省市区
    handleChange(e) {
      this.ruleForm.code = e;
      if (e) {
        this.ruleForm.province_code = e[0];
        this.ruleForm.city_code = e[1];
        this.ruleForm.country_code = e[2];
      } else {
        this.ruleForm.province_code = null;
        this.ruleForm.city_code = null;
        this.ruleForm.country_code = null;
      }
    },
    // 分页
    changeLimit(pageSize) {
      this.current.limit = pageSize;
      this.getSchoolList();
    },
    changePage(page) {
      this.current.page = page;
      this.getSchoolList();
    },
    // logo图片上传成功钩子
    successLogo(response, file, fileList, key) {
      let dist = {
        name: file.name,
        url: response.data.src,
      };
      this.$nextTick(() => {
        this.ruleForm[key].push(dist);
      }, 1000);
    },
    // 点击文件列表中已上传的文件时的钩子
    handlePictureCardPreview() {},
    // 图片移除时的钩子
    handleRemove() {},
    // 图片上传失败的钩子
    onError() {},
    // 文件上传时的钩子
    onProgress() {},

    // 跳转至学校详情
    navTo(row) {
      this.$router.push({
        path: "/order/list/see/" + row.id,
      });
    },

    // 新增学校的表单提交
    addSchool: Debounce(function () {
      let logo = JSON.stringify(this.ruleForm.logo);
      let qualifications = JSON.stringify(this.ruleForm.qualifications);
      let data = {
        name: this.ruleForm.name,
        code: this.ruleForm.code,
        province_code: this.ruleForm.province_code,
        city_code: this.ruleForm.city_code,
        country_code: this.ruleForm.country_code,
        address: this.ruleForm.address,
        manager_name: this.ruleForm.manager_name,
        mobile: this.ruleForm.mobile,
        status: this.ruleForm.status,
        logo: logo,
        qualifications: qualifications,
        desc: this.ruleForm.desc,
      };
      data.action = "add";
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          let param = Object.assign(
            {
              sign: Md5(
                objKeySort(Object.assign(apiUrl().name, data)) +
                  apiUrl().appsecret
              ),
            },
            apiUrl().name,
            data
          );
          addSchool(param).then((res) => {
            if (res.errcode == 0) {
              this.$message.success(res.errmsg || "操作成功");
              this.closeCard();
              this.getSchoolList();
            } else {
              this.$message.error(res.errmsg || "操作失败");
            }
          });
        }
      });
    }, 1000),
    // 关闭模态框
    closeCard() {
      this.$refs.ruleForm.resetFields();
      this.schoolDialog = false;
    },

    changeSwitch: Debounce(function (e, row) {
      this.disabled = true;
      let data = {
        action: "uptown",
        id: row.id,
        status: e,
      };
      let query = Object.assign(
        {
          sign: Md5(
            objKeySort(Object.assign(apiUrl().name, data)) + apiUrl().appsecret
          ),
        },
        apiUrl().name,
        data
      );
      addSchool(query).then((res) => {
        if (res.errcode == 0) {
          this.$message.success(res.errmsg);
          this.disabled = false;
        }
      });
    }),

    // 账号
    account(param) {
      this.$router.push({ path: "/order/list/account", query: { id: param } });
    },
  },
};
</script>
<style lang="scss" scoped>
@import "@/styles/variables.scss";

.main_ti {
  margin-bottom: 15px;
  padding-bottom: 1px;
  position: relative;

  /deep/.el-tabs__header {
    margin: 0;
  }

  /deep/.el-checkbox {
    margin-top: 12px;
  }

  &::after {
    height: 1px;
    content: "";
    position: absolute;
    left: 0;
    bottom: 0;
    width: 100%;
    background-color: #e4e7ed;
    z-index: 1;
  }

  /deep/.el-tabs__nav-wrap::after {
    height: 0;
  }
}

/deep/.el-tag--small {
  height: 20px;
  line-height: 20px;
}

.mini_form {
  margin: 0 10px;
  border: 1px solid #e1e1e1;
}

.input_width {
  width: 350px;
}

.imgCard {
  display: flex;
  flex-direction: row;
  justify-content: flex-start;
  flex-wrap: wrap;

  img {
    width: 80px;
    height: 80px;
  }
}
</style>
